package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDTO userDTO){
        PageResult<UserDO> pageResult = userService.list(userDTO);
        return Result.success(pageResult);
    }

    @LogAnno
    @PostMapping
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

    @LogAnno
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        userService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        UserDO userDO = userService.get(id);
        return Result.success(userDO);
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/role/{roleLabel}")
    public Result getUsersByRole(@PathVariable String roleLabel){
        List<UserDO> userDOList = userService.getUsersByRole(roleLabel);
        return Result.success(userDOList);
    }

    @GetMapping("/dept/{deptId}")
    public Result getUsersByDept(@PathVariable Integer deptId){
        List<UserDO> userDOList = userService.getUsersByDept(deptId);
        return Result.success(userDOList);
    }
}
